﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Liquid;
using Common;
using Common.Admin;
using Common.Controls;

namespace SLAdmin.UserControls
{
    public partial class StudyList : UserControl
    {
        StudyManager _studyManager = StudyManager.GetInstance();
        StudyDataClient _studyDataClient = new StudyDataClient();
        DialogHelper _dialogHelper = new DialogHelper();

        public StudyList()
        {
            InitializeComponent();
            
            PopulateTreeView();
        }
        private Dictionary<string,bool> _treeViewState = new Dictionary<string,bool>();
        private Type _selectedObjectType;
        private string _selectedObjectId;
        private void SaveTreeViewState(List<Node> Nodes)
        {
            foreach (Node myNode in Nodes)
            {
                if (myNode.DataContext != null)
                {
                    if (_treeViewState.Keys.Contains(myNode.DataContext.GetType().ToString() + "_" + myNode.Tag.ToString()) == false)
                    {
                        _treeViewState.Add(myNode.DataContext.GetType().ToString() + "_" + myNode.Tag.ToString(), myNode.IsExpanded);
                    }
                }
                else
                {
                    if (_treeViewState.Keys.Contains(myNode.Name) == false)
                    {
                        _treeViewState.Add(myNode.Name, myNode.IsExpanded);
                    }
                }
                if (myNode.Nodes.Count > 0)
                {
                    SaveTreeViewState(myNode.Nodes.ToList());
                }
            }
        }        
        private void ApplyTreeViewState(List<Node> Nodes)
        {
            foreach (Node myNode in Nodes)
            {
                if (myNode.DataContext != null && _treeViewState.Keys.Contains(myNode.DataContext.GetType().ToString() + "_" + myNode.Tag.ToString()))
                {
                    //Get expanded state
                    myNode.IsExpanded = _treeViewState[myNode.DataContext.GetType().ToString() + "_" + myNode.Tag.ToString()];                        
                }
                else if (_treeViewState.Keys.Contains(myNode.Name))
                {
                    myNode.IsExpanded = _treeViewState[myNode.Name];
                }
                if (myNode.Nodes.Count > 0)
                {
                    ApplyTreeViewState(myNode.Nodes.ToList());
                }
            }
        }
        public void PopulateTreeView()
        {
            //TODO: REWRITE ROUTINE TO ADD/UPDATE/REORDER as necessary instead of deleting
            //them all and starting over.
            
            //Remember the selected item
            
            //Remember the viewstate for this tree
            //Clear the list first
            _treeViewState.Clear();
            if (studyList.Nodes != null)
            {
                SaveTreeViewState(studyList.Nodes.ToList());
            }
            //Repopulate the nodes
            studyList.Nodes.Clear();
            SLAdmin.App myApp = SLAdmin.App.GetApplication;
            Node UserNode = new Node();
            UserNode.Tag = "0";
            UserNode.Title = "Studies";
            UserNode.Icon = "icons/db-16.png";
            UserNode.DataContext = App.GetApplication;
            UserNode.Tag = 1;
            studyList.Nodes.Add(UserNode);
            // Iterate through the studies
            foreach (Study myStudy in _studyManager.Studies)
            {
                Node Root = new Node();
                Root.Tag = myStudy.StudyId.ToString();                
                Root.Title = myStudy.StudyName;
                Root.DataContext = myStudy;
                Root.Icon = "icons/StudyIcon2_15.png";                
                UserNode.Nodes.Add(Root);
                List<Condition> ConditionList = (from x in _studyManager.Conditions
                                                 where x.StudyId == myStudy.StudyId
                                                 select x).ToList();
                // Iterate through the Conditions
                foreach (Condition myCondition in ConditionList)
                {
                    Node ConditionNode = new Node();
                    ConditionNode.Tag = myCondition.ConditionId;
                    ConditionNode.Title = myCondition.ConditionName;
                    ConditionNode.DataContext = myCondition;
                    ConditionNode.Icon = "icons/Condition_15.png";
                    Root.Nodes.Add(ConditionNode);
                    List<StudyWave> Waves = (from x in _studyManager.StudyWaves
                                             where x.StudyId == myStudy.StudyId
                                             select x).ToList();
                    // Iterate through the Waves
                    foreach (StudyWave Wave in Waves)
                    {
                        Node WaveNode = new Node();
                        WaveNode.Tag = Wave.StudyWaveId;
                        WaveNode.Title = Wave.StudyWaveName;
                        WaveNode.DataContext = Wave;
                        WaveNode.Icon = "icons/Column-Chart-16x16.png";
                        ConditionNode.Nodes.Add(WaveNode);
                        //Iterate through the surveys (via StudyWaveSurvey)
                        List<StudyWaveSurvey> StudyWaveSurveys =
                            (from x in _studyManager.StudyWaveSurveys
                             where x.StudyWaveId == Wave.StudyWaveId
                             && x.Conditionid == myCondition.ConditionId
                             orderby x.DisplayOrder
                             select x).ToList();
                        foreach (StudyWaveSurvey mySws in StudyWaveSurveys)
                        {
                            Survey mySurvey = (from y in _studyManager.Surveys
                                               where y.SurveyId == mySws.SurveyId                                                       
                                               select y).SingleOrDefault();
                            Node SurveyNode = new Node();
                            SurveyNode.Title = mySurvey.SurveyName;
                            SurveyNode.Tag = mySurvey.SurveyId;
                            SurveyNode.DataContext = mySurvey;
                            SurveyNode.Icon = "icons/Survey_15.png";
                            WaveNode.Nodes.Add(SurveyNode);
                            //Iterate through the survey pages (via SurveySurveyPage)
                            List<SurveySurveyPage> SurveySurveyPages =
                                (from x in _studyManager.SurveySurveyPages
                                 where x.SurveyId == mySurvey.SurveyId
                                 orderby x.DisplayOrder
                                 select x).ToList();
                            foreach (SurveySurveyPage mySsp in SurveySurveyPages)
                            {
                                SurveyPage Page = (from y in _studyManager.SurveyPages
                                                   where y.SurveyPageId == mySsp.SurveyPageId
                                                   select y).SingleOrDefault();
                                Node SurveyPageNode = new Node();
                                SurveyPageNode.Title = Page.PageName;
                                SurveyPageNode.Tag = Page.SurveyPageId;
                                SurveyPageNode.DataContext = Page;
                                SurveyPageNode.Icon = "icons/Page_15.png";
                                SurveyNode.Nodes.Add(SurveyPageNode);

                            }
                        }
                    }
                }
            }
            //Populate treeview again
            ApplyTreeViewState(studyList.Nodes.ToList());
            //Clear old values
            _treeViewState.Clear();

        }
        public delegate void StudySelectedDelegate(Study Study);
        public event StudySelectedDelegate StudySelected;
        public delegate void ConditionSelectedDelegate(Condition Condition, Study Study);
        public event ConditionSelectedDelegate ConditionSelected;
        public delegate void StudyWaveSelectedDelegate(StudyWave StudyWave, Condition Condition);
        public event StudyWaveSelectedDelegate StudyWaveSelected;
        public delegate void SurveySelectedDelegate(Survey Survey);
        public event SurveySelectedDelegate SurveySelected;
        public delegate void SurveyPageSelectedDelegate(SurveyPage SurveyPage);
        public event SurveyPageSelectedDelegate SurveyPageSelected;
        public delegate void StudiesSelectedDelegate(List<Study> Studies);
        public event StudiesSelectedDelegate StudiesSelected;
        private void studyList_NodeClick(object sender, TreeEventArgs e)
        {        
            
            Node selectedNode = sender as Node;
            object selectDataObject = selectedNode.DataContext;
            if (selectDataObject is Study)
            {
                Study myStudy = (Study)selectDataObject;
                StudySelected(myStudy);
                
            }
            else if (selectDataObject is Condition)
            {
                Condition myCondition = (Condition)selectDataObject;
                Study myStudy = (Study)(selectedNode.ParentNode.DataContext);
                ConditionSelected(myCondition, myStudy);
            }
            else if (selectDataObject is StudyWave)
            {
                StudyWave myStudyWave = (StudyWave)selectDataObject;
                Condition myCondition = selectedNode.ParentNode.DataContext as Condition;
                StudyWaveSelected(myStudyWave, myCondition);
            }
            else if (selectDataObject is Survey)
            {
                Survey mySurvey = (Survey)selectDataObject;
                SurveySelected(mySurvey);
            }
            else if (selectDataObject is SurveyPage)
            {
                SurveyPage mySurveyPage = (SurveyPage)selectDataObject;
                SurveyPageSelected(mySurveyPage);
            }
            else if (selectedNode == studyList.Nodes[0])
            {
                StudiesSelected(_studyManager.Studies);
            }            
        }
    }
}
